package com.aistar.service.impl;

import com.aistar.mapper.ResumeDeliveryRecordMapper;
import com.aistar.pojo.ResumeDeliveryRecord;
import com.aistar.pojo.ResumeDeliveryRecordExample;
import com.aistar.service.ResumeDeliveryRecordService;
import com.aistar.util.DateUtil;
import com.aistar.util.MyBatisUtil;
import com.aistar.util.ServerResponse;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ResumeDeliveryRecordServiceImpl implements ResumeDeliveryRecordService {
    @Override
    public ServerResponse save(ResumeDeliveryRecord resumeDeliveryRecord) {

        ServerResponse sr =  getRecordInMonth(resumeDeliveryRecord.getJobId(),resumeDeliveryRecord.getResumeId());
        if(sr.getStatus() ==1){ //有记录，不能重复投递，即不添加
            return  ServerResponse.addFailed() ;  // 0添加失败，有记录，不能重复投递，即不添加

        }else{//没有记录，即不添加投递
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            ResumeDeliveryRecordMapper mapper = sqlSession.getMapper(ResumeDeliveryRecordMapper.class);
            int rows = mapper.insert(resumeDeliveryRecord);
            sqlSession.commit();
            MyBatisUtil.closeSqlSession();
            return  ServerResponse.addSuccess(); //1 添加成功，投递成功
        }




    }

    public static void main(String[] args) {
        new ResumeDeliveryRecordServiceImpl().getRecordInMonth(12,85);
    }
    //查询一个月之内 不能投递重复的岗位
    public ServerResponse getRecordInMonth(Integer jobId,Integer resumeId){
        System.out.println("jobId:"+jobId);
        System.out.println("resumeId:"+resumeId);
        //       select * from .... where job_id = ? and resumeId = ?  and  现在时间-发送的时间<一个月


        //  现在时间-发送的时间 <=一个月
        // 现在时间-一个月 <=发送的时间  2.3<=2.4
        System.out.println(DateUtil.getBeforeDate(-30));
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        System.out.println("sqlsession is closed? :" +sqlSession);
        ResumeDeliveryRecordMapper mapper = sqlSession.getMapper(ResumeDeliveryRecordMapper.class);
        ResumeDeliveryRecordExample example = new ResumeDeliveryRecordExample();

        example.createCriteria().andJobIdEqualTo(jobId)
                                .andResumeIdEqualTo(resumeId)
                                .andDeliveryTimeGreaterThanOrEqualTo(DateUtil.getBeforeDate(-30));


        List<ResumeDeliveryRecord> list = mapper.selectByExample(example);
        MyBatisUtil.closeSqlSession();

        if(list!=null && list.size()>0){ // 有投递记录
           return ServerResponse.getDataSuccess(list); // 1
        }else{
            return ServerResponse.getDataFailed("没有投递记录");// 0
        }


    }






    @Override
    public ServerResponse modifyStatus(ResumeDeliveryRecord resumeDeliveryRecord) {
        return null;
    }

    @Override
    public ServerResponse modify(ResumeDeliveryRecord resumeDeliveryRecord) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
        return null;
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }
}
